package test.serverframe.armc.server.manager.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;

import test.serverframe.armc.server.manager.domain.BindParamToInterface;
import test.serverframe.armc.server.manager.domain.LinkInterface;

import java.util.List;

public interface LinkInterfaceMapper {
    @Delete({
            "delete from tb_ssgp_link_interface",
            "where node_id = #{nodeId,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String nodeId);

    @Insert({
            "insert into tb_ssgp_link_interface (node_id, parent_ids, child_ids ",
            "interface_id)",
            "values (#{nodeId,jdbcType=VARCHAR}, #{parentIds,jdbcType=VARCHAR}, #{childIds,jdbcType=VARCHAR} ",
            "#{interfaceId,jdbcType=VARCHAR})",
    })
    int insert(LinkInterface record);

    @InsertProvider(type = LinkInterfaceSqlProvider.class, method = "insertSelective")
    int insertSelective(LinkInterface record);

    @Select({
            "select",
            "node_id, parent_ids, child_ids, interface_id",
            "from tb_ssgp_link_interface",
            "where node_id = #{nodeId,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "node_id", property = "nodeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "parent_ids", property = "parentId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "child_ids", property = "childIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "interface_id", property = "interfaceId", jdbcType = JdbcType.VARCHAR),
    })
    LinkInterface selectByPrimaryKey(String nodeId);

    @Select({
            "select",
            "*",
            "from tb_ssgp_link_interface",
            "where node_id = #{nodeId,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "node_id", property = "nodeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "parent_id", property = "parentId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "child_ids", property = "childIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "interface_id", property = "interfaceId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "node_id", property = "linkParam",
                    one = @One(
                            select = "test.serverframe.armc.server.manager.dao.mapper.LinkParamMapper.selectByNodeId",
                            fetchType = FetchType.LAZY
                    )
            )
    })
    LinkInterface selectAllByPrimaryKey(String nodeId);

    @Select({
            "select",
            "*",
            "from tb_ssgp_link_interface",
            "where parent_id = #{nodeId,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "node_id", property = "nodeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "parent_id", property = "parentId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "child_ids", property = "childIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "interface_id", property = "interfaceId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "node_id", property = "linkParam",
                    one = @One(
                            select = "test.serverframe.armc.server.manager.dao.mapper.LinkParamMapper.selectByNodeId",
                            fetchType = FetchType.LAZY
                    )
            )
    })
    List<LinkInterface> selectAllParentId(String nodeId);

    @UpdateProvider(type = LinkInterfaceSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(LinkInterface record);

    @Update({
            "update tb_ssgp_link_interface",
            "set parent_id = #{parentId,jdbcType=VARCHAR},",
            "child_ids = #{childIds,jdbcType=VARCHAR},",
            "interface_id = #{interfaceId,jdbcType=VARCHAR},",
            "where node_id = #{nodeId,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(LinkInterface record);

    @Select({
            "SELECT li.interface_id,li.node_id,li.parent_ids,li.child_ids,",
            "lp.in_header,lp.in_body,lp.in_query",
            "FROM tb_ssgp_link_interface AS li, tb_ssgp_link_param AS lp",
            "WHERE lp.node_id = li.node_id AND li.node_id IN (",
            "SELECT tei.node_id",
            "FROM tb_ssgp_task_enter_interface AS tei",
            "WHERE tei.task_id = #{taskId,jdbcType=VARCHAR}",
            ")"
    })
    @Results({
            @Result(column = "node_id", property = "nodeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "parent_id", property = "parentId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "child_ids", property = "childIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "interface_id", property = "interfaceId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "in_header", property = "inHeader", jdbcType = JdbcType.VARCHAR),
            @Result(column = "in_body", property = "inBody", jdbcType = JdbcType.VARCHAR),
            @Result(column = "in_query", property = "inQuery", jdbcType = JdbcType.VARCHAR)
    })
    List<BindParamToInterface> selectInterfaceByTaskId(String taskId);

    @Select({
            "SELECT li.interface_id,li.node_id,li.parent_ids,li.child_ids,",
            "lp.in_header,lp.in_body,lp.in_query",
            "FROM tb_ssgp_link_interface AS li, tb_ssgp_link_param AS lp",
            "WHERE lp.node_id = li.node_id AND li.parent_id = #{nodeId,jdbcType=VARCHAR}",
    })
    @Results({
            @Result(column = "node_id", property = "nodeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "parent_id", property = "parentId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "child_ids", property = "childIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "interface_id", property = "interfaceId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "in_header", property = "inHeader", jdbcType = JdbcType.VARCHAR),
            @Result(column = "in_body", property = "inBody", jdbcType = JdbcType.VARCHAR),
            @Result(column = "in_query", property = "inQuery", jdbcType = JdbcType.VARCHAR)
    })
    List<BindParamToInterface> selectInterfaceByNodeId(String nodeId);
}
